package com.gykfbxxh.imovie.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.AppBaseAdapter;
import com.gykfbxxh.imovie.bean.film.FilmEntity;
import com.gykfbxxh.imovie.greendao.GDManager;
import com.gykfbxxh.imovie.utils.glide.ImageLoader;
import com.gykfbxxh.imovie.view.filmsdetail.acitivity.FilmDetailActivity;

import java.util.ArrayList;
import java.util.List;

import static com.gykfbxxh.imovie.base.BaseActivity.PARAMETER1;

/**
 * Created by xuxinghai
 * On 2017/8/25
 */

public class FilmsAdapter extends AppBaseAdapter<FilmEntity> {
    private String type;//当前适配的栏目类型
    private View.OnClickListener onClick;
    List<String> likeData = null ;//用户收藏本地数据库列表

    public FilmsAdapter(List<FilmEntity> list, Context context, String type) {
        super(list, context);
        this.type = type;
        getLikeLookData();
    }

    private void getLikeLookData() {
        likeData = GDManager.getInstance().getLikeLookListData();
        if(likeData==null) {
            likeData = new ArrayList<>();
        }
    }

    public FilmsAdapter(List<FilmEntity> list, Context context, String type, View.OnClickListener onClick) {
        super(list, context);
        this.type = type;
        this.onClick = onClick;
        getLikeLookData();
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_newslist_video, null);
            holder = new ViewHolder();
            holder.item_layout = (LinearLayout) convertView.findViewById(R.id.item_layout);
            holder.ll_impression = (LinearLayout) convertView.findViewById(R.id.ll_impression);
            holder.iv_imageview = (ImageView) convertView.findViewById(R.id.iv_imageview);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_title_sub = (TextView) convertView.findViewById(R.id.tv_title_sub);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.tv_score = (TextView) convertView.findViewById(R.id.tv_score);
            holder.tv_likelook = (CheckBox) convertView.findViewById(R.id.tv_likelook);
            holder.tv_comment = (TextView) convertView.findViewById(R.id.tv_comment);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        FilmEntity filmEntity = list.get(position);
        if(filmEntity==null) {
            filmEntity = new FilmEntity();
        }
        holder.item_layout.setTag(R.id.tag_first,filmEntity);
        holder.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FilmEntity film = (FilmEntity) v.getTag(R.id.tag_first);
                if(film==null|| TextUtils.isEmpty(film.getFId())) {
                    return;
                }
                Intent intent = new Intent(context, FilmDetailActivity.class);
                intent.putExtra(PARAMETER1, film.getFId());
                context.startActivity(intent);
            }
        });
        ImageLoader.getInstance().listLoadImage2Cache(context,holder.iv_imageview,filmEntity.getFPhoto());
        holder.tv_title.setText(filmEntity.getFName());
        holder.tv_title_sub.setText(filmEntity.getFKeyword());
        holder.tv_time.setText(filmEntity.getFYear()+"年上映 · "+filmEntity.getFCountryname());
        holder.tv_type.setText(filmEntity.getFFilmtypename());
        holder.tv_score.setText(filmEntity.getFScore()+"");

        if(TextUtils.isEmpty(filmEntity.getFImpression())) {
            holder.ll_impression.setVisibility(View.GONE);
        }else {
            holder.ll_impression.setVisibility(View.VISIBLE);
            holder.tv_comment.setText(filmEntity.getFImpression());
        }

        holder.tv_likelook.setTag(R.id.tag_first,filmEntity);
        holder.tv_likelook.setTag(R.id.tag_secong,false);
        holder.tv_likelook.setChecked(false);
        holder.tv_likelook.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,R.drawable.look),null,null,null);
        if(likeData!=null&&likeData.size()>0) {
            for (int i =0;i<likeData.size();i++){
                if(filmEntity.getFId().equals(likeData.get(i))) {
                    holder.tv_likelook.setChecked(true);
                    holder.tv_likelook.setTag(R.id.tag_secong,true);
                    holder.tv_likelook.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,R.drawable.unlook),null,null,null);
                    break;
                }
            }
        }
        holder.tv_likelook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FilmEntity filmEntity = (FilmEntity) v.getTag(R.id.tag_first);
                boolean isChecked = (boolean) v.getTag(R.id.tag_secong);
                if(isChecked) {//想看>>>>不想看
                    ((CheckBox)v).setChecked(false);
                    ((CheckBox)v).setTag(R.id.tag_secong,false);
                    ((CheckBox)v).setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,R.drawable.look),null,null,null);
                    likeData.remove(filmEntity.getFId());
                    GDManager.getInstance().delLikeLookData(filmEntity.getFId());
                }else {//不想看>>>>想看
                    ((CheckBox)v).setChecked(true);
                    ((CheckBox)v).setTag(R.id.tag_secong,true);
                    ((CheckBox)v).setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context,R.drawable.unlook),null,null,null);
                    likeData.add(filmEntity.getFId());
                    GDManager.getInstance().insertLikeLookData(filmEntity);
                }
            }
        });

        return convertView;
    }


    class ViewHolder{
        LinearLayout item_layout;
        LinearLayout ll_impression;
        ImageView iv_imageview;
        TextView tv_title;
        TextView tv_title_sub;
        TextView tv_time;
        TextView tv_type;
        TextView tv_score;
        CheckBox tv_likelook;
        TextView tv_comment;
    }
}
